package com.lh.controller;

import com.lh.entity.Cart;
import com.lh.entity.User;
import com.lh.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: LuoHang
 * @Date: 2021/2/12 21:48
 * @Description
 */
@Controller
public class CartController {
    @Autowired
    private CartService cartService;

    @RequestMapping("/getCartHtml")
    public ModelAndView getCartHtml(@RequestParam Integer userId, HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute("user");
        Map<String,Integer> data = new HashMap<>(2);
        data.put("id",userId);
        data.put("status",1);
        user.setCartBuyNums(cartService.getCartNums(data));
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("cartList", cartService.getCarts(userId));
        modelAndView.setViewName("cart");
        return modelAndView;
    }

    @RequestMapping("/addCart")
    @ResponseBody
    public Map<String, Object> addCart(@RequestParam Integer userId, @RequestParam Integer goodsId, @RequestParam Integer buyNum) {
        Map<String, Object> res = new HashMap<>(1);
        boolean flag = true;
        List<Cart> carts = cartService.getCarts(userId);
        for (Cart cart : carts) {
            if (cart.getGoods().getId().equals(goodsId)) {
                //获取cart的id
                Map<String, Object> query = new HashMap<>(2);
                query.put("userId", userId);
                query.put("goodsId", goodsId);
                Cart cart1 = cartService.getCartByUserIdGoodsId(query);

                Map<String, Object> updateInfo = new HashMap<>(2);
                updateInfo.put("id", cart1.getId());
                updateInfo.put("buyNum", cart.getBuyNum() + buyNum);
                int i = cartService.updateCart(updateInfo);
                if (i > 0) {
                    flag = false;
                    res.put("data", "添加成功");
                } else {
                    res.put("data", "添加失败");
                }
                break;
            }
        }
        if (flag) {
            Map<String, Object> map = new HashMap<>(3);
            map.put("userId", userId);
            map.put("goodsId", goodsId);
            map.put("buyNum", buyNum);
            map.put("status",1);
            int count = cartService.addCart(map);
            res.put("data", count > 0 ? "添加成功" : "添加失败");
        }
        return res;
    }

    @RequestMapping("/delCart")
    @ResponseBody
    public Map<String, Object> delCart(@RequestParam Integer userId, @RequestParam Integer goodsId, @RequestParam Integer buyNum) {
        Map<String, Object> res = new HashMap<>(1);
        Map<String,Object> map = new HashMap<>(2);
        map.put("userId",userId);
        map.put("goodsId",goodsId);
        Cart cart = cartService.getCartByUserIdGoodsId(map);
        if (cart.getBuyNum().equals(1)){
            //如果数量为零的时候，直接删除掉
            int i = cartService.deleteCartById(cart.getId());
            if (i > 0) {
                res.put("data", "操作成功");
            } else {
                res.put("data", "操作失败");
            }
        }else {
            //如果数量不为零就做减减
            Map<String, Object> updateInfo = new HashMap<>(2);
            updateInfo.put("id", cart.getId());
            updateInfo.put("buyNum", cart.getBuyNum() - buyNum);
            int i = cartService.updateCart(updateInfo);
            if (i > 0) {
                res.put("data", "操作成功");
            } else {
                res.put("data", "操作失败");
            }
        }
        return res;
    }


    @RequestMapping("/deleteCartById")
    @ResponseBody
    public Map<String, Object> deleteCartById(@RequestParam Integer id) {
        int count = cartService.deleteCartById(id);
        Map<String, Object> map = new HashMap<>(1);
        map.put("data", count > 0 ? "删除成功" : "删除失败");
        return map;
    }

    public int update(Map<String, Object> map) {
        return cartService.updateCart(map);
    }

    /**
     * 前台获取个人中心页面
     *
     * @return 视图模型
     */
    @RequestMapping("/getPersonalHtml")
    public ModelAndView getPersonalHtml() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("personal");
        return modelAndView;
    }
}
